package com.nccsjz.front.web;

import java.sql.SQLException;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.nccsjz.back.customer.service.CustomerService;
import com.nccsjz.back.link.service.LinkService;
import com.nccsjz.back.merch.service.MerchService;
import com.nccsjz.back.vote.service.VoteService;
import com.nccsjz.base.BaseAction;
import com.nccsjz.front.service.IndexCardService;
import com.nccsjz.pojo.BatExportFhtb;
import com.nccsjz.pojo.Bulletin;
import com.nccsjz.pojo.CardWay;
import com.nccsjz.pojo.Faq;
import com.nccsjz.pojo.Link;
import com.nccsjz.pojo.Merchant;
import com.nccsjz.pojo.News;
import com.nccsjz.pojo.Vote;

public class IndexCardAction extends BaseAction {

	private Bulletin bulletin;

	private List<News> news;// 新闻

	private List<Faq> faqs;// 常见问题

	private List<CardWay> wds;// 用卡知识

	private List<Merchant> merchs;// 商户联盟

	private List<Link> links;//友情链接
	
	private String mname;// 用户名

	private String mpwd;// 密码

	private String startDate;

	private String endDate;
	

	private List<BatExportFhtb> bats;// 消费记录查询
	private String JIAOFEI = "801";// 缴费
	private String ZHUANZHIXIAOFEI = "701";// 转值消费
	private String ZHICHONGZHI = "702";// 转值充值
	private String LJ = "3351";// 充资交易
	private String LJSHOUKAJIAOYI = "3352";// 售卡交易
	private String LJCHONGZIJIAOYI = "3310";// 充资交易
	private String LJXF = "8401";// 联机消费（电子帐户）

	private long uid;// 用户ID
	private Object merch;// 商户或卡户信息,变量名改为user更好，但是为了复用只能如此。

	public Object getMerch() {
		return merch;
	}

	public void setMerch(Object merch) {
		this.merch = merch;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public List<BatExportFhtb> getBats() {
		return bats;
	}

	public void setBats(List<BatExportFhtb> bats) {
		this.bats = bats;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getMname() {
		return mname;
	}

	public void setMname(String mname) {
		this.mname = mname;
	}

	public String getMpwd() {
		return mpwd;
	}

	public void setMpwd(String mpwd) {
		this.mpwd = mpwd;
	}

	public List<Merchant> getMerchs() {
		return merchs;
	}

	public void setMerchs(List<Merchant> merchs) {
		this.merchs = merchs;
	}

	private Vote vo;

	//private long[] answerid;// 答案ID
	private Long answerid;

	private List propics;

	public List getPropics() {
		return propics;
	}

	public void setPropics(List propics) {
		this.propics = propics;
	}

//	public long[] getAnswerid() {
//		return answerid;
//	}
//
//	public void setAnswerid(long[] answerid) {
//		this.answerid = answerid;
//	}
	
	

	private IndexCardService service;
	
	public Long getAnswerid() {
		return answerid;
	}

	public void setAnswerid(Long answerid) {
		this.answerid = answerid;
	}

	private LinkService linkService;

	private final int topN = 6;// 前 6条记录

	public Bulletin getBulletin() {
		return bulletin;
	}

	public void setBulletin(Bulletin bulletin) {
		this.bulletin = bulletin;
	}

	public List<News> getNews() {
		return news;
	}

	public void setNews(List<News> news) {
		this.news = news;
	}

	public List<Faq> getFaqs() {
		return faqs;
	}

	public void setFaqs(List<Faq> faqs) {
		this.faqs = faqs;
	}

	public List<CardWay> getWds() {
		return wds;
	}

	public void setWds(List<CardWay> wds) {
		this.wds = wds;
	}

	public Vote getVo() {
		return vo;
	}

	public void setVo(Vote vo) {
		this.vo = vo;
	}	

	public List<Link> getLinks() {
		return links;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	// 首次进入，初始化首页显示相关信息。
	public String firstEnter_Index() throws SQLException {
		this.service = new IndexCardService();
		bulletin = service.getLastBulletin();// 公告

		news = service.getTopNews(topN);// 前6条新闻
		if (news != null && news.size() < 1) {
			this.addActionMessage("暂时还没有新闻");
		}

		faqs = service.getTopFaqs(topN);// 前6条常见问题
		if (faqs != null && faqs.size() < 1) {
			this.addActionMessage("暂时还没有");
		}

		wds = service.getTopCardWays(topN);// 前6条用卡知识
		if (wds != null && wds.size() < 1) {
			this.addActionMessage("暂时还没有");
		}

		merchs = service.getTopCardMerchs(topN);

		try {
			vo = service.getSelectedVote();// 投票相关
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//获取所有友情链接信息
		linkService = new LinkService();
		links = linkService.finalAllLinks();

		return "firstEnter";
	}

	/**
	 * updateAnswerClickPlusOne方法用于更新投票答案的点击数量
	 * 
	 * @param voteAnswerId
	 *            投票答案编号
	 * @throws SQLException
	 */
	public String clickVote_Index() {
		VoteService voteService = new VoteService();
		try {
			if(answerid != null){
				voteService.updateAnswerClickCountPlusOne(answerid);				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "vote";
	}

	/***
	 * 投票结果
	 * 
	 * @return
	 * @throws Exception
	 */
	public String voteResult_Index() throws Exception {
		this.service = new IndexCardService();
		vo = service.getSelectedVote();
		return "voteResult";
	}

	/***
	 * 得到前台产品展示的图片
	 */
	public String getProPics_Index() {
		this.service = new IndexCardService();
		this.propics = service.getProPics(ServletActionContext
				.getServletContext());
		return "pro";
	}

	public String login_Index() {
		this.service = new IndexCardService();
		int st = 0;
		try {
			st = service.checkUser(mname, mpwd);
			if (st > 0) {
				this.getSession().put("sid", service.getUserID(mname, mpwd));
				this.getSession().put("smname", mname);				
				this.getSession().put("st", st);
				//如果是商户则检查该商户是否审核通过
				if(st == 2){
					boolean flag = false;
					if(this.service.isMerchantPass(mname, mpwd)){
						flag = true;
					}
					this.getSession().put("isPass",flag);
				}
			} else {
				this.addActionError("用户名或密码不正确！");
			}

		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError("用户名或密码不正确！");
		}
		this.getSession().put("st", st);
		return "index";
	}

	/**
	 * 根据st 得到用户信息（卡户或商户）
	 * 
	 * @throws Exception
	 * 
	 */
	public String userInfo_Index() throws Exception {
		String path = "userinfo";
		
		// 卡户
		if (Integer.valueOf(getSession().get("st").toString()) == 1) {
			merch = new CustomerService().getCus(uid);			
		}

		// 商户
		if (Integer.valueOf(getSession().get("st").toString()) == 2) {
			merch = new MerchService().getMerchById(uid);
			path = "merchantinfo";
		}

		return path;
	}

	/***
	 * 缴费查询
	 * 
	 * @return
	 */
	public String storeM_Index() {

		return "";
	}

	/**
	 * 消费查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String buyM_Index() throws Exception {
		service = new IndexCardService();
		// 转值消费 联机消费（电子帐户）
		this.bats = service.buySomething(startDate, endDate, this.getSession()
				.get("smname").toString(), "");
		return "buy";
	}

	/**
	 * 缴费查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String payoffM_Index() throws Exception {
		service = new IndexCardService();
		// 缴费
		this.bats = service.payoffSomething(startDate, endDate, this
				.getSession().get("smname").toString(), JIAOFEI);
		return "payoff";
	}

	/**
	 * 充值查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addmoneyM_Index() throws Exception {
		service = new IndexCardService();
		// 充值
		this.bats = service.payoffSomething(startDate, endDate, this
				.getSession().get("smname").toString(), ZHICHONGZHI);
		return "addmoney";
	}
	
}
